﻿using System.Text;
using System.Web;
using System.Web.Mvc;

namespace iss.sg4210.se18.team6s.Views.Utility
{

    public static class MenuHelper
    {
        public static string Menu(this HtmlHelper helper)
        {
            var sb = new StringBuilder();

            SiteMapNodeCollection topLevelNodes = SiteMap.RootNode.ChildNodes;
            ParseSiteConfig(topLevelNodes, sb, helper);
            return sb.ToString();
        }

        private static string ParseSiteConfig(SiteMapNodeCollection nodes, StringBuilder sb, HtmlHelper helper)
        {
            // Create opening unordered list tag
            sb.Append("<ul>");

            // Render each top level node
            foreach (SiteMapNode node in nodes)
            {
                sb.AppendLine("<li>");

                sb.AppendFormat("<a href='{0}'>{1}</a>", node.Url, helper.Encode(node.Title));
                sb.AppendLine("</li>");

                if (node.ChildNodes.Count > 0)
                    ParseSiteConfig(node.ChildNodes, sb, helper);
            }

            // Close unordered list tag
            sb.Append("</ul>");

            return sb.ToString();
        }
    }
}